#include "functor/SubscriberSlot.h"
#include "event/Event.h"
#include "event/BoundSlot.h"
namespace KCore
{
	BoundSlot::BoundSlot(int group, const SubscriberSlot& subscriber, Event& event) :
	_group(group),
	_subscriber(new SubscriberSlot(subscriber)),
	_event(&event)
	{}

	BoundSlot::BoundSlot(const BoundSlot& other) :
	_group(other._group),
	_subscriber(other._subscriber),
	_event(other._event)
	{
	}

	BoundSlot::~BoundSlot()
	{
		disconnect();
		delete _subscriber;
	}


	BoundSlot& BoundSlot::operator=(const BoundSlot& other)
	{
		_group      = other._group;
		_subscriber = other._subscriber;
		_event      = other._event;

		return *this;
	}


	bool BoundSlot::operator==(const BoundSlot& other) const
	{
		return _subscriber == other._subscriber;
	}


	bool BoundSlot::operator!=(const BoundSlot& other) const
	{
		return !(*this == other);
	}

	bool BoundSlot::connected() const
	{
		return (_subscriber != 0) && _subscriber->connected();
	}


	void BoundSlot::disconnect()
	{
		// cleanup the bound subscriber functor
		if (connected())
			_subscriber->cleanup();

		// remove the owning Event's reference to us
		if (_event)
		{
			// get the event to erase the subscriber functor.
			_event->unsubscribe(*this);
			_event = 0;
		}

	}
};
